{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8420148d",
   "metadata": {},
   "source": [
    "# 1. 导入需要的requests模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "29952ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cfac425",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0. 人脸API 的第一步，获取人脸face_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d598442",
   "metadata": {},
   "source": [
    "# 2.输入我们需要API网站注册的API_Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cb64800",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"klsjAb3laLyeLzoud0XphWdrtkWn96Tj\"\n",
    "API_secret = \"7C2OwuO1Nb-pmLYAZi0wWWjCVVf-Ms2h\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c3dda7b",
   "metadata": {},
   "source": [
    "# 3.准备API测试接口的url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "492df120",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91606412",
   "metadata": {},
   "source": [
    "# 4. 调用方法 POST"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "478e10ed",
   "metadata": {},
   "source": [
    "# 5.准备url传递参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e07a5245",
   "metadata": {},
   "outputs": [],
   "source": [
    "paylod = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fm.imeitou.com%2Fuploads%2Fallimg%2F2018071211%2Fhic5aj3x4hu.jpg&refer=http%3A%2F%2Fm.imeitou.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649902987&t=57d6d4f7279ec2ad971a1dbd2c205355\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b042f6f6",
   "metadata": {},
   "source": [
    "# 6. 发起请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1d377676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647311414,96a87850-b519-4801-9423-f9b881b8479d',\n",
       " 'time_used': 235,\n",
       " 'faces': [{'face_token': 'fbdafed099db88decc1007c63f385158',\n",
       "   'face_rectangle': {'top': 147, 'left': 57, 'width': 146, 'height': 146},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 19},\n",
       "    'smile': {'value': 0.068, 'threshold': 50.0}}}],\n",
       " 'image_id': 'WiZRFb92ZoUKUdQFX6WvTw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = requests.post(url=face_detect,params=paylod)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38380ab3",
   "metadata": {},
   "source": [
    "# 1.人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6c349351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'klsjAb3laLyeLzoud0XphWdrtkWn96Tj'\n",
    "API_sercret = '7C2OwuO1Nb-pmLYAZi0wWWjCVVf-Ms2h'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'fbdafed099db88decc1007c63f385158',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "489c75a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647311414,96a87850-b519-4801-9423-f9b881b8479d',\n",
       " 'time_used': 235,\n",
       " 'faces': [{'face_token': 'fbdafed099db88decc1007c63f385158',\n",
       "   'face_rectangle': {'top': 147, 'left': 57, 'width': 146, 'height': 146},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 19},\n",
       "    'smile': {'value': 0.068, 'threshold': 50.0}}}],\n",
       " 'image_id': 'WiZRFb92ZoUKUdQFX6WvTw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dd61d64",
   "metadata": {},
   "source": [
    "# 2.人脸对比API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2bd22d2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'klsjAb3laLyeLzoud0XphWdrtkWn96Tj'\n",
    "API_sercret = '7C2OwuO1Nb-pmLYAZi0wWWjCVVf-Ms2h'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':'fbdafed099db88decc1007c63f385158',\n",
    "    'face_tokens1':\n",
    "    'face_tokens2':\n",
    "    \n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n",
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5b5900b",
   "metadata": {},
   "source": [
    "# 封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec853abe",
   "metadata": {},
   "source": [
    "1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9d232822",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_dectect(API_key,API_sercret,image_url):\n",
    "    \"\"\" face++ 人脸检测\"\"\"\n",
    "    url='https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "         'api_key':API_key,\n",
    "         'api_secret':API_sercret,\n",
    "         'image_url':image_url,\n",
    "        'return_lankmark':lankmark,\n",
    "        'return_attributes':attributes\n",
    "        \n",
    "    }\n",
    "    r=requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63116b22",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
